package com.bdqn.t382.lx.interceptors;

import com.bdqn.t382.lx.pojo.SysUser;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;

public class AuthInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        SysUser user = (SysUser) session.getAttribute("user");
        String path = request.getServletPath();
        if (user != null) {
            Long roleId = user.getRoleId();
            if (roleId == 1) {
                return true;
            } else {
                if ("/book/page".equals(path)) {
                    request.getRequestDispatcher("/book/list").forward(request, response);
                    return false;
                }
                return true;
            }
        } else {
            response.sendRedirect("/bookManager/erro.jsp");
            return false;
        }
    }
}